package no.apero.wiiamp;

import java.io.*;
import java.util.Properties;

import org.directwebremoting.*;

import no.apero.wiiamp.exceptions.DriveException;


public class FileReader {
	
	private static FileReader fileReader = null;
	private String clampPath = "";

	private File[] roots;
	private File currentDir;
	private String confPath;
	private Properties prop = new Properties();

	
	private FileReader() {
		confPath = WebContextFactory.get().getServletContext().getRealPath("/WEB-INF/conf.properties");
		try {
			prop.load(new FileInputStream(confPath));
			clampPath = prop.getProperty("clampPath");
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
		roots = File.listRoots();
		currentDir = roots[0];
	}

	public static FileReader getInstance() {
		if (fileReader != null) {
			return fileReader;
		} else {
			fileReader = new FileReader();
		}
		return fileReader;
	}

	
	public File getCurrentDir() {
		return currentDir;
	}

	private class DirFilter implements FileFilter {
		public boolean accept(File f) {
			return f.isDirectory();
		}
		public String getDescription() {
			return null;
		}
	}
	private class MyFileFilter implements FileFilter {
		public boolean accept(File f) {
			return f.isFile();
		}
		public String getDescription() {
			return null;
		}
	}
	private class MediaFileFilter implements FilenameFilter {
		public boolean accept(File f, String s) {
			return s.endsWith(".mp3") 
			| s.endsWith(".wma");
		}
		public String getDescription() {
			return null;
		}
	}

	private class ClampFileFilter implements FilenameFilter {
		public boolean accept(File f, String s) {
			return s.equals("CLAmp.exe");
		}
		public String getDescription() {
			return null;
		}
	}
	
	public File[] listDirs() {
		return currentDir.listFiles(new DirFilter());
	}

	public File[] listFiles() {
		return currentDir.listFiles(new MyFileFilter());
	}

	public File[] listMediaFiles() {
		return currentDir.listFiles(new MediaFileFilter());
	}

	public File listClampFiles() {
		try {
			return currentDir.listFiles(new ClampFileFilter())[0];
		} catch (ArrayIndexOutOfBoundsException e) {
			return null;
		} catch (NullPointerException e) {
			return null;
		}
	}

	public File[] listDrives() {
		return this.roots;
	}

	
	
	public void goUp() throws DriveException{
		if (currentDir.getParentFile()!=null){
			currentDir = currentDir.getParentFile();
		} else {
			throw new DriveException();
		}
	}
	
	public void goInto(String folder){
		if (currentDir ==null){
			currentDir = new File(folder); 
		} else {
			currentDir = new File(currentDir.getAbsolutePath() + "\\" + folder); 
		}
	}

	public void setDrive(String drive) throws Exception{
		currentDir = new File(drive + "\\"); 
		if (currentDir == null){
			throw new Exception();
		}
	}


	public String getClampPath() {
		return clampPath;
	}

	public void setClampPath(String clampPath) {
		this.clampPath = this.currentDir + "\\" + clampPath;
		prop.setProperty("clampPath", this.clampPath);
		try {
			prop.store(new FileOutputStream(confPath), "nada");
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
}
